///<reference path="../BaseLifecycleObj.ts"/>
///<reference path="../../../../../game/utils/ResizeUtils.ts"/>
/**
 * Created by PengWei on 2018/7/25.
 */
namespace mengdao {
    import GComponent = fairygui.GComponent;
    import ResourceEvent = RES.ResourceEvent;
    import UIPackage = fairygui.UIPackage;

    export abstract class BaseView<T extends GComponent> extends BaseLifecycleObj implements IView {
        private static readonly DYNAMIC_RES_PREFIX = 'dynamicResPrefix_'
        private _inLoading=false;
        private _hasInit=false;
        private _ui: T;

        protected abstract get packageName():string
        protected get resName():string{//若你的导出类名字不一样，请覆写这个方法
            return this.packageName;
        }
        public get ui(): T {
            return this._ui;
        }

        public addToParent(parent: GComponent, idx: number = -1): void {
            if (idx == -1) {
                parent.addChild(this._ui);
            } else {
                parent.addChildAt(this._ui, idx);
            }
        }

        public removeFromParent(): void {
            if (this._ui.parent) {
                this._ui.parent.removeChild(this._ui);
            }
        }

        public initialize(): void {
            // this._ui = this.uiClass['createInstance']()
            // this.onInit();
        }

        public active(): void {
            if(this._hasInit){
                super.active();
                this.onUpdate();
                this.onResize();
            }else if(!this._inLoading){
                this._inLoading = true;
                let pkgName = this.packageName;
                let groupName = BaseView.DYNAMIC_RES_PREFIX+pkgName
                let resList = [pkgName]
                let atlasName = pkgName+'@atlas0';
                if(RES.hasRes(atlasName)) resList.push(atlasName)
                // resList.push(atlasName)
                RES.createGroup(groupName,resList)
                let gotResGroup = (evt:ResourceEvent)=>{
                    if(evt.groupName!=groupName) return;
                    RES.removeEventListener(ResourceEvent.GROUP_COMPLETE, gotResGroup, this)
                    global[pkgName][pkgName+'Binder'].bindAll();
                    UIPackage.addPackage(pkgName);
                    this._ui = <any>fairygui.UIPackage.createObject(this.packageName, this.resName);
                    let btnClose = this._ui.getChild('btnClose')
                    if(btnClose) btnClose.addClickListener(this.close, this)
                    this.onInit();
                    this._readyCall.apply(this._callObj);
                }
                RES.addEventListener(ResourceEvent.GROUP_COMPLETE, gotResGroup, this);
                RES.loadGroup(groupName);
            }
        }

        public sleep(): void {
            super.sleep();
        }

        public close(): void {
            this.sleep();
            this.onClose();
            this.removeFromParent();
        }

        abstract onInit(): void;

        abstract onUpdate(): void;

        abstract onClose(): void;

        abstract onResize(): void;

        //---------------------------------------------------------------------------------------------------------
        //                                          对显示对象的基础属性转接
        //---------------------------------------------------------------------------------------------------------
        public getWidth(): number {
            //TODO 提供多种提供宽的方式
            if (this._ui)
                return this._ui.width;
            return 0;
        }

        public getHeight(): number {
            //TODO 提供多种提供高的方式
            if (this._ui)
                return this._ui.height;
            return 0;
        }

        public set scale(value: number) {
            if (this._ui)
                this._ui.scaleX = this._ui.scaleY = value;
        }

        public set x(value: number) {
            if (this._ui) this._ui.x = value;
        }

        public set y(value: number) {
            if (this._ui) this._ui.y = value;
        }

        public set width(value: number) {
            if (this._ui) this._ui.width = value;
        }

        public set height(value: number) {
            if (this._ui) this._ui.height = value;
        }

        private _readyCall:Function;
        private _callObj:any;
        __setViewReadyCall(callBack:Function,callObj:any){
            this._readyCall = callBack;
            this._callObj = callObj;
        }
    }
}